<template>
  <div class="article-page">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>面经后台</el-breadcrumb-item>
      <el-breadcrumb-item>面经管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <div slot="header" class="clearfix header">
        <span>共 {{ total }} 条记录</span>
        <el-button icon="el-icon-plus" type="primary" round @click="add"
          >添加面经</el-button
        >
      </div>
      <el-table :data="articleList" style="width: 100%">
        <el-table-column prop="stem" label="标题" width="400"></el-table-column>
        <el-table-column prop="creator" label="作者"> </el-table-column>
        <el-table-column prop="likeCount" label="点赞"> </el-table-column>
        <el-table-column prop="views" label="浏览数"> </el-table-column>
        <el-table-column prop="createdAt" label="更新时间" width="200">
        </el-table-column>
        <el-table-column label="操作" prop="articleList">
          <template v-slot="scoped">
            <div class="actions">
              <i class="el-icon-view" @click="look(scoped.row.id)"></i>
              <i class="el-icon-edit-outline" @click="edit(scoped.row.id)"></i>
              <i class="el-icon-delete" @click="del(scoped.row.id)"></i>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        layout="prev, pager, next"
        :total="total"
        :page-size="5"
        @current-change="change"
      >
      </el-pagination>
    </el-card>
    <!-- 查看面经 -->
    <el-drawer title="面经预览" :visible.sync="drawer1" size="50%">
      <div class="article-preview">
        <h5>{{ detail.stem }}</h5>
        <div v-html="detail.content"></div>
      </div>
    </el-drawer>
    <!-- 增和改面经 -->
    <el-drawer
      :title="this.articleFrorm.id ? '修改面经' : '添加面经'"
      :visible.sync="drawer2"
      size="50%"
      @close="close"
    >
      <el-form
        :model="articleFrorm"
        ref="articleFrorm"
        label-width="80px"
        class="articleFrorm"
        :rules="rules"
      >
        <el-form-item label="标题" prop="stem">
          <el-input
            v-model="articleFrorm.stem"
            placeholder="输入面经标题"
          ></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <quill-editor v-model="articleFrorm.content" @blur="contentChangeFn">
          </quill-editor>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="queren" @click="subFrom()"
            >确认</el-button
          >
          <el-button @click="close">取消</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
  </div>
</template>

<script>
import {
  getArticleAPI,
  getDetailAPI,
  delArticleAPI,
  postArticleAPI,
  putArticleAPI
} from '@/api/article'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import { quillEditor } from 'vue-quill-editor'
export default {
  name: 'ArticleView',
  components: {
    quillEditor
  },
  data () {
    return {
      params: {
        current: 1,
        pageSize: 5
      },
      articleList: [],
      total: 0,
      drawer1: false,
      drawer2: false,
      detail: {},
      articleFrorm: {
        stem: '',
        content: '',
        id: null
      },
      rules: {
        stem: [{ required: true, message: '请输入面经标题', trigger: 'blur' }],
        content: [
          { required: true, message: '请输入面经内容', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    async getArticleList () {
      const { data: res } = await getArticleAPI(this.params)
      // console.log(res)
      this.articleList = res.rows
      this.total = res.total
    },
    change (value) {
      this.params.current = value
      this.getArticleList()
    },
    // 自己封装触发element-ui的表单校验
    contentChangeFn () {
      this.$refs.articleFrorm.validateField('content')
    },
    // 查看
    async look (id) {
      this.drawer1 = true
      const res = await getDetailAPI({ id })
      // console.log(res)
      this.detail = res.data
    },
    // 添加
    add () {
      this.drawer2 = true
    },
    // 修改
    async edit (id) {
      const res = await getDetailAPI({ id })
      this.drawer2 = true
      // console.log(res)
      // 解决先点修改后点添加,表单未重置问题
      this.$nextTick(() => {
        this.articleFrorm.stem = res.data.stem
        this.articleFrorm.content = res.data.content
        this.articleFrorm.id = id
      })
    },
    subFrom () {
      this.$refs.articleFrorm.validate(async valid => {
        if (!valid) return
        try {
          if (this.articleFrorm.id) {
            await putArticleAPI(this.articleFrorm)
            this.$message.success('修改成功')
          } else {
            await postArticleAPI({
              stem: this.articleFrorm.stem,
              content: this.articleFrorm.content
            })
            this.$message.success('添加成功')
          }
          this.getArticleList()
          this.drawer2 = false
        } catch {
          return this.$message.error('请求失败')
        }
      })
    },
    // 点击取消
    close () {
      this.articleFrorm.id = null
      this.$refs.articleFrorm.resetFields()
      this.drawer2 = false
    },
    async del (id) {
      await delArticleAPI({ id })
      this.$message.success('删除成功')
      this.getArticleList()
    }
  },
  created () {
    this.getArticleList()
  }
}
</script>

<style lang="less" scoped>
.el-breadcrumb {
  margin: 10px 0 25px;
}
.header {
  line-height: 32px;
  button {
    float: right;
    padding: 9px 16px;
    margin-right: 16px;
    font-size: 12px;
    background-color: #8e96f7;
    border-color: #8e96f7;
  }
}
.actions {
  display: flex;
  justify-content: space-around;
  i {
    font-size: 18px;
  }
}
.el-pagination {
  text-align: center;
  margin-top: 20px;
  ::v-deep .active {
    background: #727cf5 !important;
  }
}
.article-preview {
  padding: 40px;
  padding-top: 0;

  h5 {
    font-size: 20px;
    margin-bottom: 20px;
    padding-bottom: 30px;
    color: #666;
    border-bottom: 1px dashed #ccc;
  }
}
.articleFrorm {
  height: 100%;
  padding-right: 40px;
  ::v-deep .ql-editor {
    height: 300px;
  }
  .queren {
    background-color: #727cf5;
    border-color: #727cf5;
  }
}
</style>
